package com.china08.yxyapi.api.home.activity.marker;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.maker.YxyMakerWorks;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.marker.YxyMakerWorksRepository;

/**
 * 区域教育局-竞赛报名-报名列表
 * 
 */
@CrossOrigin
@RestController
public class YxyMakerWorksPageController extends YxyApiBaseController {

    @Autowired
    private YxyMakerWorksRepository yxyMakerWorksRepository;

    @ResponseBody
    @RequestMapping(value = "/api_yw/home/activity/competition/catalog/works/page", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Page<RespModel> precess(HttpServletRequest request, HttpServletResponse response,
            @RequestBody ReqModel reqModel) throws ParseException {
        // 得到当前登录用户
        Pageable pageable = new PageRequest(reqModel.page, reqModel.size, new Sort(Direction.DESC, "createdDate"));
        Page<YxyMakerWorks> page = null;
        if ("all".equals(reqModel.phase)) {// 当学段为全部时
            if ("all".equals(reqModel.code)) {// 当作品来源为全部时
                if (StringUtils.isNotBlank(reqModel.keyword)) {
                    page = yxyMakerWorksRepository.findByNameLike("%" + reqModel.keyword + "%", pageable);
                } else {
                    page = yxyMakerWorksRepository.findAll(pageable);
                }
            } else {// 当选择作品来源时
                if (StringUtils.isNotBlank(reqModel.keyword)) {
                    page = yxyMakerWorksRepository.findByDistrictCodeAndNameLike(reqModel.code,
                            "%" + reqModel.keyword + "%", pageable);
                } else {
                    page = yxyMakerWorksRepository.findByDistrictCode(reqModel.code, pageable);
                }
            }
        } else {// 当选择学段时
            if ("all".equals(reqModel.catalog1)) {// 当一级分类为全部时
                if ("all".equals(reqModel.code)) {// 当作品来源为全部时
                    if (StringUtils.isNotBlank(reqModel.keyword)) {
                        page = yxyMakerWorksRepository.findByPhaseAndNameLike(reqModel.phase,
                                "%" + reqModel.keyword + "%", pageable);
                    } else {
                        page = yxyMakerWorksRepository.findByPhase(reqModel.phase, pageable);
                    }
                } else {// 当选择作品来源时
                    if (StringUtils.isNotBlank(reqModel.keyword)) {
                        page = yxyMakerWorksRepository.findByPhaseAndDistrictCodeAndNameLike(reqModel.phase,
                                reqModel.code, "%" + reqModel.keyword + "%", pageable);
                    } else {
                        page = yxyMakerWorksRepository.findByPhaseAndDistrictCode(reqModel.phase, reqModel.code,
                                pageable);
                    }
                }

            } else {// 当选择一级分类时
                if ("all".equals(reqModel.catalog2)) {// 当二级分类为全部时
                    if ("all".equals(reqModel.code)) {// 当作品来源为全部时
                        if (StringUtils.isNotBlank(reqModel.keyword)) {
                            page = yxyMakerWorksRepository.findByPhaseAndCatalog1AndNameLike(reqModel.phase,
                                    reqModel.catalog1, "%" + reqModel.keyword + "%", pageable);
                        } else {
                            page = yxyMakerWorksRepository.findByPhaseAndCatalog1(reqModel.phase, reqModel.catalog1,
                                    pageable);
                        }
                    } else {// 当选择作品来源时
                        if (StringUtils.isNotBlank(reqModel.keyword)) {
                            page = yxyMakerWorksRepository.findByPhaseAndCatalog1AndDistrictCodeAndNameLike(
                                    reqModel.phase, reqModel.catalog1, reqModel.code, "%" + reqModel.keyword + "%",
                                    pageable);
                        } else {
                            page = yxyMakerWorksRepository.findByPhaseAndCatalog1AndDistrictCode(reqModel.phase,
                                    reqModel.catalog1, reqModel.code, pageable);
                        }
                    }

                } else {// 当选择二级分类时
                    if ("all".equals(reqModel.code)) {// 当作品来源为全部时
                        if (StringUtils.isNotBlank(reqModel.keyword)) {
                            page = yxyMakerWorksRepository.findByPhaseAndCatalog1AndCatalog2AndNameLike(reqModel.phase,
                                    reqModel.catalog1, reqModel.catalog2, "%" + reqModel.keyword + "%", pageable);
                        } else {
                            page = yxyMakerWorksRepository.findByPhaseAndCatalog1AndCatalog2(reqModel.phase,
                                    reqModel.catalog1, reqModel.catalog2, pageable);
                        }
                    } else {// 当选择作品来源时
                        if (StringUtils.isNotBlank(reqModel.keyword)) {
                            page = yxyMakerWorksRepository.findByPhaseAndCatalog1AndCatalog2AndDistrictCodeAndNameLike(
                                    reqModel.phase, reqModel.catalog1, reqModel.catalog2, reqModel.code,
                                    "%" + reqModel.keyword + "%", pageable);
                        } else {
                            page = yxyMakerWorksRepository.findByPhaseAndCatalog1AndCatalog2AndDistrictCode(
                                    reqModel.phase, reqModel.catalog1, reqModel.catalog2, reqModel.code, pageable);
                        }
                    }

                }
            }
        }
        HashMap<String, String> map = new HashMap<>();
        map.put("1", "小学");
        map.put("2", "初中");
        map.put("3", "高中");
        map.put("4", "中职");
        int num = 0;
        List<RespModel> models = new ArrayList<RespModel>();
        for (YxyMakerWorks work : page) {
            num++;
            RespModel respModel = new RespModel();
            respModel.num = (pageable.getPageNumber() * page.getSize()) + num;
            respModel.id = work.getId();
            respModel.name = work.getName();
            respModel.phase = map.get(work.getPhase());
            respModel.type = work.getCatalogname1() + "/" + work.getCatalogname2();
            respModel.school = work.getSchoolName();
            respModel.url1 = work.getRegisterFile();
            respModel.url2 = work.getWorksFile();
            models.add(respModel);
        }
        PageImpl<RespModel> pageImpl = new PageImpl<RespModel>(models, pageable, page.getTotalElements());
        Page<RespModel> pageLst = pageImpl;
        return pageLst;

    }

    public static class ReqModel extends YxyApiPageReqModel {
        // 所属学段 小学：1 初中：2 高中：3 中职：4 全部：all
        public String phase;
        // 作品分类id 全部：all
        public String catalog1;
        // 项目名称id 全部：all
        public String catalog2;
        // 作品来源 全部：all
        public String code;
        // 作品名称
        public String keyword;
    }

    protected static class RespModel extends YxyApiRespModel {
        // 序号
        public int num;
        // 作品id
        public String id;
        // 作品名称
        public String name;
        // 所属学段
        public String phase;
        // 作品分类
        public String type;
        // 作品来源
        public String school;
        // 报名文件路径
        public String url1;
        // 作品文件路径
        public String url2;
    }

}
